package aliasMatching.controller;

import aliasMatching.model.Alias;
import aliasMatching.model.DisplayInfo;
import aliasMatching.model.MyTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import rdfparser.db.DBConnection;

/**
 *
 * @author amendrashrestha
 */
public final class TimeEquality {

    List userList;
    List username;
    public static List secondUserTimeList;
    public static List firstUserTimeList;
    List timeList = new ArrayList();
    public List<Alias> aliases;
    private List postTime;
    //static Connection con = null;

    public TimeEquality(List userList) {
        this.userList = userList;
        aliases = new ArrayList<Alias>();
    }

    public TimeEquality() {
    }

    public TimeEquality(List userIDList, List username, MyTableModel mdl) throws SQLException {
        this.userList = userIDList;
        this.username = username;

        for (int i = 1; i < userList.size(); i++) {
            //for (int j = i + 1; j < userList.size(); j++) {
            double tempDistance = 0.0;
            int firstUser = Integer.parseInt(userList.get(0).toString());
            int otherUser = Integer.parseInt(userList.get(i).toString());

            tempDistance = getTimeVarience((firstUser), otherUser);
            String euclideanDistance = Double.toString(tempDistance);
            float euclideanPercentage = (float) tempDistance * 100;

            /*String common = "";
             List temp = (List) timeList;
             if (temp.isEmpty()) {
             common = "0";
             } else {
             for (Object k : temp) {
             if (common.length() > 0) {
             common += ",";
             }
             common += k;
             }
             }
             int totalCommonPostTimeCount = temp.size();
             float equalPercentage;
             float selectedNodePostTime = firstUserTimeList.size() + secondUserTimeList.size() + 1;
             //System.out.println(selectedNodeFriends);
             equalPercentage = ((totalCommonPostTimeCount * 100) / selectedNodePostTime);*/

            DisplayInfo di = new DisplayInfo(username.get(0).toString(),
                    userList.get(i).toString(),
                    username.get(i).toString(),
                    euclideanDistance, euclideanPercentage, "Time");
            mdl.SetValue(di);
            // }
        }
    }

    /*
     * This method returns year and time posted by any users.
     */
    public static List getPostTime(int ID) {
        List<Timestamp> time = new ArrayList();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection con = null;
        try {
            con = DBConnection.getDBConnection();
            String queryTime = "select created from tbl_posts where `MakerID` = \'" + ID + "\'";
            //System.out.println("Connection: " + con);
//                    System.out.println("Query: " + queryTime);
//                    System.out.println("");
            ps = con.prepareStatement(queryTime);
            rs = ps.executeQuery();
            while (rs.next()) {
                time.add(rs.getTimestamp(1));
            }
        } catch (java.lang.Throwable t) {
             System.out.println(t.getClass().getName()); //this'll tell you what class has been thrown
//            t.printStackTrace(); //get a stack trace
        } finally {
            try {
                if (con != null) {                
                    con.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ex) {
            }
        }
        return time;
    }

    public static List<String> getAllPost(int ID) throws SQLException {
        List messages = new ArrayList();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection con = null;

        try {
            con = DBConnection.getDBConnection();
            //System.out.println("Connection: " + con);
            String querypostID = "SELECT `Content` FROM `tbl_posts` WHERE `MakerID` =\'" + ID + "\'";
            ps = con.prepareStatement(querypostID);
            rs = ps.executeQuery();
            //System.out.println("User: " + ID);
            while (rs.next()) {
                messages.add(rs.getString(1));
                //System.out.println("Message text: " + rs.getString(1));
                //System.out.println();
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ex) {
            }
        }
        return messages;
    }

    /*
     * This method return the difference between the post between our test users.
     */
    public double getTimeVarience(int firstID, int secondID) throws SQLException {

        //firstUserTimeList = getPostTime(firstID);
        //secondUserTimeList = getPostTime(secondID);

        double[] tempTimeVector1 = getTimeVector(firstID);
        double[] tempTimeVector2 = getTimeVector(secondID);

        double sum1 = 0;
        double sum2 = 0;
        for (int j = 0; j < tempTimeVector1.length; j++) {
            sum1 = sum1 + tempTimeVector1[j];
            sum2 = sum2 + tempTimeVector2[j];
        }
        for (int j = 0; j < tempTimeVector1.length; j++) {
            tempTimeVector1[j] = tempTimeVector1[j] / sum1;
            tempTimeVector2[j] = tempTimeVector2[j] / sum2;
        }

        double euclideanDistance = 0.0;
        euclideanDistance = TimeStylomTest.calculateTimeVector(tempTimeVector1, tempTimeVector2);

        /* List timeVarience = new ArrayList();

         for(Object firstList : firstUserTimeList){

         }

         /*firstUserTimeList = getPostTime(firstID);
         secondUserTimeList = getPostTime(secondID);

         List timeVarience = new ArrayList();

         for (Object firstList : firstUserTimeList) {

         Timestamp first = Timestamp.valueOf(firstList.toString());
         for (Object secondList : secondUserTimeList) {
         Timestamp second = Timestamp.valueOf(secondList.toString());
         long tsTime1 = (first.getTime()) / 60000;
         long tsTime2 = (second.getTime()) / 60000;
         if (Math.abs(tsTime1 - tsTime2) < 120) {
         timeVarience.add(first);
         }
         }
         }*/
        return euclideanDistance;
    }

    public double[] getTimeVector(int ID) throws SQLException {

        postTime = getPostTime(ID);
        double[] rr = new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        Iterator itr = postTime.iterator();

        while (itr.hasNext()) {
            Timestamp key = (Timestamp) itr.next();
            int hr = key.getHours();
            /**
             * just taking in consideration if user has posted on that hour of
             * day or not
             */
            /*if(rr[hr] == 0){
             rr[hr]++;
             }*/
            rr[hr]++;
        }
        return rr;
    }
}
